package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.pojo.SysLogininfor;
import com.cl.short_video_java.service.SysLogininforService;
import com.cl.short_video_java.mapper.SysLogininforMapper;
import com.cl.short_video_java.util.DateUtils;
import com.cl.short_video_java.util.ExceUtil;
import com.cl.short_video_java.util.IpUtils;
import com.cl.short_video_java.vo.sys.LoginInforExceSearch;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author object
 * @description 针对表【sys_logininfor(系统访问记录)】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class SysLogininforServiceImpl extends ServiceImpl<SysLogininforMapper, SysLogininfor>
		implements SysLogininforService {


	@Resource
	private SysLogininforMapper sysLogininforMapper;
	private ExceUtil ExcelUtil;

	/**
	 * 添加登录日志
	 *
	 * @param request
	 * @param username
	 */
	@Override
	public void addLog(HttpServletRequest request, String username, String desc) {
		String ipAddr = IpUtils.getIpAddr(request);
		SysLogininfor sysLogininfor = new SysLogininfor();
		String hostName = IpUtils.getHostName();
		String hostIp = IpUtils.getHostIp();
		String ipRegion = IpUtils.getIpRegion(hostIp);
		Map<Object, Object> browserType = IpUtils.getBrowserType(request);

		String browserType1 = getBrowserType(request);


		sysLogininfor.setUserName(username);
		sysLogininfor.setIpaddr(ipAddr);
		sysLogininfor.setBrowser(browserType1);
		sysLogininfor.setOs((String) browserType.get("sysInfo"));
		sysLogininfor.setStatus(0);
		sysLogininfor.setMsg(desc);
		sysLogininfor.setLoginTime(new Date());
		sysLogininfor.setLoginLocation(ipRegion);

		this.save(sysLogininfor);
	}

	private String getBrowserType(HttpServletRequest request) {
		String browserType = request.getHeader("User-Agent").toLowerCase();
		String type = "ie";
		System.out.println("browerType:" + browserType);
		if (browserType.indexOf("msie") > 0) {
			type = "ie";
		}
		if (browserType.indexOf("firefox") > 0) {
			type = "firefox";
		}
		if (browserType.indexOf("edg") > 0) {
			type = "edge";
		}
		if (browserType.indexOf("chrome") > 0) {
			type = "chrome";
		}
		if (browserType.indexOf("opera") > 0) {
			type = "opera";
		}
		if (browserType.indexOf("gecko") > 0 && browserType.indexOf("rv:11") > 0) {
			type = "ie11";
		}
		return type;
	}

	@Override
	public Boolean removeLogininforService(String idlist) {
		String[] split = idlist.split(",");
		boolean removeByIds = this.removeByIds(Arrays.stream(split).toList());
		return removeByIds;
	}

	@Override
	@Transactional
	public Boolean clearLoginInforService() {
		return this.remove(null);
	}

	@Override
	public PageInfo<SysLogininfor> pageLogininforService(LoginInforExceSearch loginInforExceSearch) {
		if (loginInforExceSearch.getPageNum() == null || loginInforExceSearch.getPageSize() == null) {
			loginInforExceSearch.setPageNum(1);
			loginInforExceSearch.setPageSize(20);
		}
		PageHelper.startPage(loginInforExceSearch.getPageNum(), loginInforExceSearch.getPageSize());
		List<SysLogininfor> sysLogininfors = sysLogininforMapper.selectList(new QueryWrapper<SysLogininfor>()
				.eq(!loginInforExceSearch.getIpaddr().equals(""), "ipaddr", loginInforExceSearch.getIpaddr())
				.eq(!loginInforExceSearch.getUserName().equals(""), "user_name", loginInforExceSearch.getUserName())
				.eq(loginInforExceSearch.getStatus() != null, "status", loginInforExceSearch.getStatus())
				.between((loginInforExceSearch.getBeginTime() != null && loginInforExceSearch.getEndTime() != null), "login_time", loginInforExceSearch.getBeginTime(), loginInforExceSearch.getEndTime())
				.orderByDesc("login_time"));
		PageInfo<SysLogininfor> pageInfo = new PageInfo<>(sysLogininfors);
		return pageInfo;
	}

	@Override
	public ResponseEntity<byte[]> getExceService(LoginInforExceSearch loginInforSearch) throws IOException {
		List<SysLogininfor> list = this.list();
		if (loginInforSearch.getIdlist().length() > 0) {
			String[] split = loginInforSearch.getIdlist().split(",");
			list = sysLogininforMapper.selectBatchIds(Arrays.stream(split).toList());
		} else {
			list = this.list(new QueryWrapper<SysLogininfor>()
					.eq(!loginInforSearch.getIpaddr().equals(""), "ipaddr", loginInforSearch.getIpaddr())
					.eq(!loginInforSearch.getUserName().equals(""), "user_name", loginInforSearch.getUserName())
					.eq(loginInforSearch.getStatus() != null, "status", loginInforSearch.getStatus())
					.between(loginInforSearch.getBeginTime() != null && loginInforSearch.getEndTime() != null, "login_time", loginInforSearch.getBeginTime(), loginInforSearch.getEndTime()));
		}
		ByteArrayOutputStream outputStream = ExcelUtil.exportSysLogininforExcel(list);
//		org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		headers.setContentDispositionFormData("syslogininfor", "登录日志.xlsx");
		return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK);
	}

	/**
	 * 获取首页的登录日志
	 *
	 * @param num
	 * @param size
	 * @return
	 */
	@Override
	public Page<SysLogininfor> homePage(Integer num, Integer size) {
		Page<SysLogininfor> loginTime = this.page(new Page<>(num, size), new QueryWrapper<SysLogininfor>().orderByDesc("login_time"));
		return loginTime;
	}
}




